<template>
  <!-- 一级路由 -->
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
  <Transition name="fade-right" mode="out-in">
        <XtxBreadItem :key="topcategory.id">{{topcategory.name}}</XtxBreadItem>

  </Transition>

      </XtxBread>
      <!-- 轮播图样式组件 -->
      <XtxCarousel
        style="height: 500px"
        :sliders="sliders"
        autoplay
      ></XtxCarousel>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topcategory.children" :key="item.id">
            <router-link :to="`/topcategory/sub/${item.id}`">
              <img :src="item.picture" alt="" />
              <p>{{ item.name }}</p>
            </router-link>
          </li>
        </ul>
      </div>
      <!-- 推荐商品 -->
      <div class="ref-goods" v-for="item in topCategoryList" :key="item.id">
        <div class="head">
          <h3>{{item.name}}</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore :path='`/category/sub/${item.id}`'/>
        </div>
        <div class="body">
          <googsItem v-for="goods in item.goods" :key="goods.id"  :goods='goods'/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findBanner } from '@/api/home'
import { computed, ref, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { findTopCategory } from '@/api/category'
import googsItem from './components/goodsItem.vue'
export default {
  name: 'indexHeader',
  components: {
    googsItem
  },
  setup () {
    console.log(12345)
    const store = useStore()
    const route = useRoute()
    const sliders = ref([])
    const topCategoryList = ref([])

    findBanner().then((data) => {
      sliders.value = data.result
    })
    // 获取数据
    const topcategory = computed(() => {
      let cate = {}
      const item = store.state.category.list.find((item) => {
        return item.id === route.params.id
      })
      if (item) cate = item
      console.log(item)
      return cate
    })

    // 商品推荐
    const getsublist = () => {
      findTopCategory(route.params.id).then(data => {
        topCategoryList.value = data.result.children
      })
    }
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/topcategory/${newVal}` === route.path) getsublist()
    }, { immediate: true })
    return { sliders, topcategory, topCategoryList }
  }
}
</script>

<style scoped lang='less'>
.fade-right-enter-from,
.fade-right-leave-to{
  opacity: 0;
  transform: translateX(20px);
}
.fade-right-enter-active,
.fade-right-leave-active{
  transition: all .5s;
}
.fade-right-enter-to,
.fade-right-leave-from{
  opacity: 1;
  transform: none;
}

.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      flex-wrap: wrap;
      padding: 0 32px;
      li {
        height: 160px;
        width: 168px;
        a {
          display: block;
          text-align: center;
          font-size: 16px;
          img {
            height: 100px;
            width: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods{
       background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head{
      .xtx-more{
          position: absolute;
        top: 20px;
        right: 20px;
      }
       .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }

    }
    .body {
      display: flex;
      // justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
